package com.yxw.blog.controller;

import com.yxw.blog.bean.entity.Article;
import com.yxw.blog.bean.response.ResponseCode;
import com.yxw.blog.bean.response.StandardResponse;
import com.yxw.blog.service.ArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.Console;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author yexinwei
 * 处理文章的控制层
 */
@Api(value = "处理文章的控制层")
@Controller
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    ArticleService articleService;

    /**
     * 删除文章
     */
    @ApiOperation(value = "删除文章接口")
    @PostMapping("/deleteArticle")
    @ResponseBody
    public StandardResponse deleteArticle(@RequestParam @ApiParam(value = "文章id") Integer articleId ) throws Exception {
        articleService.deleteArticle(articleId);
        return new StandardResponse(ResponseCode.SUCCESS,null);
    }

    /**
     *添加文章
     */
    @ApiOperation(value = "添加文章接口")
    @PostMapping("/addArticle")
    @ResponseBody
    public StandardResponse addArticle(@RequestParam @ApiParam(value = "文章标题") String title, @RequestParam @ApiParam(value = "文章内容") String content , HttpSession session) throws Exception {
        articleService.addArticle(title,content,session);
        return new StandardResponse(ResponseCode.SUCCESS,null);
    }

    /**
     * 异步上传图片
     * @return 返回layui规定的格式
     */
    @PostMapping("/uploadPhoto")
    @ResponseBody
    public Map<String,Object> Uploadimgs(@RequestParam MultipartFile file , HttpSession session) {
        Map<String,Object> map = articleService.uploadImage(file,session);
        return map;
    }

    /**
     * 图片回显
     */
    @RequestMapping(value = "/showimg/{name}")
    @ResponseBody
    public void loadrichtext(@PathVariable String name, HttpServletResponse response , HttpSession session) {
        articleService.loadrichtext(name,response,session);
    }

    /**
     * 主页显示所有的文章
     */
    @ApiOperation(value = "主页显示所有文章接口")
    @PostMapping("/showArticleList")
    @ResponseBody
    public StandardResponse showArticleList(){
        List<Article> article = articleService.showArticleList();
        return new StandardResponse<List>(ResponseCode.SUCCESS,article);
    }

    /**
     * 显示我的文章
     */
    @ApiOperation(value = "我的文章接接口")
    @PostMapping("/showMyArticleList")
    @ResponseBody
    public StandardResponse showMyArticleList(HttpSession session){
        List<Article> article = articleService.showMyArticleList(session);
        return new StandardResponse<List>(ResponseCode.SUCCESS,article);
    }

    /**
     * 显示文章详细
     */
    @ApiOperation(value = "显示文章详细接口")
    @PostMapping("/showArticle")
    @ResponseBody
    public StandardResponse<Map> showArticle(@RequestParam @ApiParam(value = "文章路径") String articleUrl , @RequestParam  @ApiParam(value = "文章id") Integer articleId , HttpSession session) throws IOException {
        Map<String,Object> map = articleService.showArticle(articleUrl,articleId,session);
        return new StandardResponse<Map>(ResponseCode.SUCCESS,map);
    }

    /**
     * 点赞功能
     */
    @ApiOperation(value = "点赞接口")
    @PostMapping("/addArticleGreat")
    @ResponseBody
    public StandardResponse addGreat(@RequestParam @ApiParam(value = "点赞文章id") Integer greatArticleId , HttpSession session) throws Exception {
        articleService.addArticleGreat(greatArticleId,session);
        return new StandardResponse(ResponseCode.SUCCESS,null);
    }

    /**
     * 取消赞功能
     */
    @ApiOperation(value = "取消赞接口")
    @PostMapping("/deleteArticleGreat")
    @ResponseBody
    public StandardResponse deleteGreat(@RequestParam @ApiParam(value = "文章id") Integer deleteGreatArticleId , HttpSession session) throws Exception{
        articleService.deleterGreat(deleteGreatArticleId,session);
        return new StandardResponse(ResponseCode.SUCCESS,null);
    }

}
